/**
 * 吃碰杠操作面板
 */
class ActionPanel extends eui.Component {
	public static ONTING = "ONTING";
	public hu: how.Button;
	public chi: how.Button;
	public peng: how.Button;
	public gang: how.Button;
	public pass: how.Button;
	public ting: how.Button;
	public buttonGroup: eui.Group;
	public itemGroup: eui.Group;
	
	public operateCard:number = 0 ;
	private canDoActions: Array<PengItem> = [];
	public constructor() {
		super();
		this.skinName = "ActionPanelSkin";
		// this.left = this.right = 0;
	}

	public childrenCreated(): void {
		this.visible = this.chi.includeInLayout = this.chi.visible = this.hu.includeInLayout = this.hu.visible =
			this.peng.includeInLayout = this.peng.visible = this.gang.includeInLayout = this.gang.visible = this.ting.includeInLayout = this.ting.visible = false;
		this.chi.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onChi, this);
		this.peng.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onPeng, this);
		this.gang.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onGang, this);
		this.hu.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onHu, this);
		this.ting.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTing, this);
		this.pass.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onPass, this);
	}

	/**
	 * 玩家选择吃牌
	 */
	public onChi(): void {
		console.log(">>> ActionPanel onChi 玩家选择吃牌");
		var chiArray: Array<PengItem> = [];
		for (let i = 0; i < this.canDoActions.length; i++) {
			if (this.canDoActions[i].type == MjLogic.Prio.ChiLeft || this.canDoActions[i].type == MjLogic.Prio.ChiMiddle || this.canDoActions[i].type == MjLogic.Prio.ChiRight) {
				chiArray.push(this.canDoActions[i]);
			}
		}
		if (chiArray.length > 1) {
			this.buttonGroup.visible = false;
			this.itemGroup.visible = true;
			for (let i = 0; i < chiArray.length; i++) {
				this.itemGroup.addChild(new ActionItem(chiArray[i]));
			}
		} else {
			this.visible = false;
			WebSocketInstance.getHallSocket().send(CMDConfig.SEND_OPERATE_CARD, { cbOperateCode: chiArray[0].type  });
			WebSocketInstance.getHallSocket().send(CMDConfig.SEND_READY);
		}
	}

	/**
	 * 玩家选择碰牌
	 */
	public onPeng(): void {
		console.log(">>> ActionPanel onPeng 玩家选择碰牌");
		this.visible = false;
		WebSocketInstance.getHallSocket().send(CMDConfig.SEND_OPERATE_CARD, { cbOperateCode: MjLogic.Prio.Peng, cbOperateCard:this.operateCard });
	}

	/**
	 * 玩家选择杠牌
	 */
	public onGang(): void {
		console.log(">>> ActionPanel onGang 玩家选择杠牌");
		var gangArray: Array<PengItem> = [];
		for (let i = 0; i < this.canDoActions.length; i++) {
			if (this.canDoActions[i].type == MjLogic.Prio.Gang) {
				gangArray.push(this.canDoActions[i]);
			}
		}
		if (gangArray.length > 1) {
			this.buttonGroup.visible = false;
			this.itemGroup.visible = true;
			for (let i = 0; i < gangArray.length; i++) {
				this.itemGroup.addChild(new ActionItem(gangArray[i]));
			}
		} else {
			this.visible = false;
			if (gangArray[0].type == MjLogic.Prio.Gang) {
				WebSocketInstance.getHallSocket().send(CMDConfig.SEND_OPERATE_CARD, { cbOperateCode: gangArray[0].type, cbOperateCard: gangArray[0].data[0] });
			} else {
				WebSocketInstance.getHallSocket().send(CMDConfig.SEND_OPERATE_CARD, { cbOperateCode: gangArray[0].type });
			}
		}
	}

	/**
	 * 玩家听牌
	 */
	public static readyTing: boolean = false;
	public onTing(): void {
		console.log(">>> ActionPanel onTing 玩家听牌----------未处理");
		this.visible = false;
		var tingArray = [];
		for (let i = 0; i < this.canDoActions.length; i++) {
			if (this.canDoActions[i].type == MjLogic.Prio.Ting) {
				tingArray = this.canDoActions[i].data;
			}
		}
		WebSocketInstance.getHallSocket().send(CMDConfig.SEND_LISTEN_CARD, {});
		ActionPanel.readyTing = true;
	}

	/**
	 * 玩家选择胡牌操作
	 */
	public onHu(): void {
		console.log(">>> ActionPanel onHu 玩家选择胡牌操作");
		this.visible = false;
		for (let i = 0; i < this.canDoActions.length; i++) {
			if (this.canDoActions[i].type == MjLogic.Prio.ChiHu) {
				if (this.canDoActions[i].data[0] == 0) {
					WebSocketInstance.getHallSocket().send(CMDConfig.SEND_OPERATE_CARD, { cbOperateCode: MjLogic.Prio.ZiMo });
					return;
				} else if (this.canDoActions[i].data[0] == 1) {
					WebSocketInstance.getHallSocket().send(CMDConfig.SEND_OPERATE_CARD, { cbOperateCode: MjLogic.Prio.ChiHu });
					return;
				}
			}
		}
	}
	/**玩家选不操作 */
	public onPass(): void {
		console.log(">>> ActionPanel onPass 玩家选不操作");
		this.visible = false;
		WebSocketInstance.getHallSocket().send(CMDConfig.SEND_OPERATE_CARD, { cbOperateCode: MjLogic.Prio.Pass , cbOperateCard:this.operateCard });
	}

	/**
	 * 检查玩家需要做什么操作
	 * this.hand_0.handCards, 0, -1, this.hand_0.pengList
	 */
	public checkAction(handCard: Array<number>, outCard: number, outCardUser: number, pengList: Array<PengItem>, isCheckHu: boolean = true): void {
		console.log(">>> ActionPanel checkAction 检查玩家需要做什么操作");
		this.canDoActions = [];
		this.itemGroup.removeChildren();
		let pv:MJPlayerView = <MJPlayerView>D.App.gameView.playerList[0];
		var iCanEat = pv.chairId - 1 < 0 ? 3 : pv.chairId - 1;
		if (outCard == 0) {//游戏开始时，或者是自己摸牌的时候，检查自摸，暗杠，补杠
			if (MjLogic.checkHu(handCard, pengList) && isCheckHu) {
				this.canDoActions.push(new PengItem([0], MjLogic.Prio.ChiHu));
			}
			if (!pv.isTing) {
				if (MjLogic.checkAnGang(handCard).length > 0) {
					var data = MjLogic.checkAnGang(handCard);
					for (let i = 0; i < data.length; i++) {
						this.canDoActions.push(new PengItem([data[i], data[i], data[i], data[i]], MjLogic.Prio.Gang));
					}
				}
				if (this.checkBuGang(handCard, pengList).length > 0) {
					var data = this.checkBuGang(handCard, pengList);
					for (let i = 0; i < data.length; i++) {
						this.canDoActions.push(new PengItem([data[i], data[i], data[i], data[i]], MjLogic.Prio.Gang));
					}
				}
				if (MjLogic.checkTing(handCard, pengList).length > 0 && !pv.isTing) {
					this.canDoActions.push(new PengItem(MjLogic.checkTing(handCard, pengList), MjLogic.Prio.Ting));
				}
			}
		} else {//其他玩家出牌的时候，检查吃，碰，明杠，点炮
			if (!pv.isTing) {
				if (iCanEat == outCardUser && this.checkOpenDoor(pengList) && MjLogic.checkChi(handCard, outCard)) {
					var data = MjLogic.checkChi(handCard, outCard);
					for (let i = 0; i < data.length; i++) {
						switch (data[i]) {
							case MjLogic.Prio.ChiLeft:
								this.canDoActions.push(new PengItem([outCard, outCard + 1, outCard + 2], MjLogic.Prio.ChiLeft, outCardUser));
								break;
							case MjLogic.Prio.ChiMiddle:
								this.canDoActions.push(new PengItem([outCard - 1, outCard, outCard + 1], MjLogic.Prio.ChiMiddle, outCardUser));
								break;
							case MjLogic.Prio.ChiRight:
								this.canDoActions.push(new PengItem([outCard - 2, outCard - 1, outCard], MjLogic.Prio.ChiRight, outCardUser));
								break;
						}
					}
				}
				if (MjLogic.checkPeng(handCard, outCard)) {
					this.canDoActions.push(new PengItem([outCard, outCard, outCard], MjLogic.Prio.Peng, outCardUser));
				}
				if (MjLogic.checkGang(handCard, outCard)) {
					this.canDoActions.push(new PengItem([outCard, outCard, outCard, outCard], MjLogic.Prio.Gang, outCardUser));
				}
			}
			if (MjLogic.checkHuByPutCard(handCard, outCard, pengList)) {
				this.canDoActions.push(new PengItem([1], MjLogic.Prio.ChiHu));
			}
		}
		this.showPanel();
	}

	public showPanel(): void {
		if (this.canDoActions.length > 0) {
			this.chi.includeInLayout = this.chi.visible = this.hu.includeInLayout = this.hu.visible =
				this.peng.includeInLayout = this.peng.visible = this.gang.includeInLayout = this.gang.visible = this.ting.includeInLayout = this.ting.visible = false;
			this.buttonGroup.visible = true;
			this.itemGroup.visible = false;
			this.visible = true;
			for (let i = 0; i < this.canDoActions.length; i++) {
				switch (this.canDoActions[i].type) {
					case MjLogic.Prio.ChiLeft:
					case MjLogic.Prio.ChiMiddle:
					case MjLogic.Prio.ChiRight:
						this.chi.includeInLayout = this.chi.visible = true;
						break;
					case MjLogic.Prio.Peng:
						this.peng.includeInLayout = this.peng.visible = true;
						break;
					case MjLogic.Prio.Gang:
						this.gang.includeInLayout = this.gang.visible = true;
						break;
					case MjLogic.Prio.ChiHu:
						this.hu.includeInLayout = this.hu.visible = true;
						break;
					case MjLogic.Prio.Ting:
						this.ting.includeInLayout = this.ting.visible = true;
						break;
				}
			}
		}
	}


	public checkBuGang(handCard: Array<number>, pengList: Array<PengItem>): Array<number> {
		var result = [];
		for (var i = 0; i < pengList.length; i++) {
			if (pengList[i].type == MjLogic.Prio.Peng) {
				for (var j = 0; j < handCard.length; j++) {
					if (pengList[i].data[0] == handCard[j]) {
						result.push(handCard[j]);
					}
				}
			}
		}
		return result;
	}

	public checkOpenDoor(pengList: Array<PengItem>): boolean {
		//let gameConfig = RES.getRes("gameconfig_json");
		//if (gameConfig.isopen) {
		if (1==1) {
			return true;
		}
		for (let i = 0; i < pengList.length; i++) {
			if (pengList[i].type != MjLogic.Prio.Gang) {//只要不是暗杠，都算开门了
				return true;
			}
		}
		return false;;
	}
}
class PengItem {
	public data: Array<number> = [];
	public type: number = 0;
	public takeUser: number = 0
	public constructor(data: Array<number>, type: number, takeUser: number = 0) {
		this.data = data;
		this.type = type;
		this.takeUser = takeUser;
	};
}